﻿using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media;

namespace Poet.Converters
{
    class PenToStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var pen = (Pen)value;
            var colorName = pen == null ? string.Empty : pen.Brush.ToString();
            return colorName;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null || string.IsNullOrEmpty(value.ToString())) return null;
            try
            {
            var color = ColorConverter.ConvertFromString(value.ToString());
            return color == null ? null : new Pen {Brush = new SolidColorBrush((Color)color)};
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}
